<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>native jsonp test</title>

  <style>
    #user{
      width: 300px;
    }
    p{
      width: 300px;
    }
  </style>
</head>
<body>
  <h2>native jsonp test</h2>
  <div>Type a name and press Enter key:</div>
  <input id="user" type="text">
  <p>Will display message...</p>

  <script>
    var user = document.getElementById('user')
    var p = document.querySelector('p')
    
    function validateUser(data){
      user.style.border = '2px solid #f92'
      p.innerHTML = data.message
    }

    user.onkeydown = function (){
      var key = (window.event ? event.keyCode : event.which)
      if (key === 13) {
        //注意：以下代码仅用于测试，因为多次执行时会不断创建新的script标签
        var username = this.value
        var script = document.createElement('script')
        script.src = 'http://localhost:8000/jsonp-validateuser?callback=validateUser&user=' + encodeURI(username)
        document.body.appendChild(script)
      }
    }
  </script>
</body>
</html>